<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
        <head>
                <title>ACL VIEWER ALPHA 0.01</title>
                <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
                <meta http-equiv="Content-Style-Type" content="text/css" />
                <meta http-equiv="Content-Language" content="fr" />
                <link rel="stylesheet" type="text/css" href="themes/style.css" />


        </head>

        <body id="dt_example">
		<div id="container">

   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
	<script type="text/javascript" src="scripts.js"></script>

		
<?php
require('include/config.inc.php');

$mysqli = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_base);

if (mysqli_connect_errno()) {
    printf("Échec de la connexion : %s\n", mysqli_connect_error());
    exit();
}

/* DEBUG
echo '<pre>';
var_dump( $_POST );
echo '</pre>';
*/

/* RECUPERATION DE LA VARIABLE */
// $name = $_POST['acl_name'];
// $fw_name = $_POST['fw_name'];
// $fw_name = '0';
$src_net = $_POST['acl_src_net'];
$dst_net = $_POST['acl_dst_net'];

/* TABLE */

?>

<?php

/* CONTENU IDENTIQUE DES DIFFERENTES CONDITIONS */
$ARRAY_HEADER = "<tr><td>Nom d'ACL</td><td>N° d'ACL</td><td>FUNCTION</td><td>PROTOCOL</td><td>SRC</td><td>DST</td><td>PORT</td><td>INACTIVE</td><td>LOG</td></tr>";

/* DECLARATION COULEUR DES COLONNES */
$i = 0; 

echo "<table border='0' class='tablecss'>";
echo "<tbody>";

/* SI AUCUN CHAMPS - CAS TOUT VIDE */
if (empty($src_net) && empty($dst_net)) {
echo 'Remplir OBLIGATOIREMENT UN CHAMPS !';
}

/* SI SRC NET SELECTED - CAS #1 SEUL */
elseif (!empty($src_net) && empty($dst_net)) {
$new_num = '0';
		$list_name_all = "SELECT * FROM object where obj_name = '$src_net' OR ipaddr = '$src_net' LIMIT 1";
		$result_list_all = $mysqli->query($list_name_all);
		echo $ARRAY_HEADER;
		while ($row_all = $result_list_all->fetch_array(MYSQLI_ASSOC)) {
			if ($row_all['type'] != 'group') {
				$list_name = "SELECT * FROM acl a JOIN object o ON a.src_net = '$src_net' OR src_net = (select obj_name from object where ipaddr = '$src_net') OR src_net = (select ipaddr from object where obj_name = '$src_net') OR a.src_net = o.obj_name WHERE (o.ipaddr = '$src_net') OR (o. obj_name = '$src_net')";
				$result_list = $mysqli->query($list_name);
				while ($row = $result_list->fetch_array(MYSQLI_ASSOC)) {
					$i++; 
					if($i%2) { $tr_color = "1";	} else { $tr_color = "2"; }	
					if ($row['fw_name'] === 'FWSM_INTERNE') {
						$row["dst_port"]=str_replace("log", "", $row["dst_port"]);
						echo '<tr class="d'.$tr_color.'">';
						echo '<td>'.$row["name"].'</td><td>'.$row["rule_num"].'</td><td>'.$row["function"].'</td><td>'.$row["protocol"].'</td><td>'.$row["src_net"].'</td><td>'.$row["dst_net"].'</td><td>'.$row["dst_port"].'</td><td>'.$row["inactive"].'</td><td>'.$row["log"].'</td>';
						echo '</tr>';
					}
					elseif ($row['fw_name'] === 'R75') {
						$new_num = $row['rule_num'];
						$list_num = "SELECT *, GROUP_CONCAT(DISTINCT CONCAT_WS(  ' : ', acl.src_net, object.ipaddr ) SEPARATOR '<br>') AS src_nets, GROUP_CONCAT( DISTINCT CONCAT_WS(  ' : ', acl.dst_net, object.ipaddr ) SEPARATOR '<br>') AS dst_nets, GROUP_CONCAT(acl.dst_port SEPARATOR '<br>') AS dst_ports FROM acl LEFT JOIN object ON acl.src_net = object.obj_name OR acl.dst_net = object.obj_name WHERE rule_num =  '$new_num';";
						$result_num = $mysqli->query($list_num);
						while ($row = $result_num->fetch_array(MYSQLI_ASSOC)) {
						$i++; 
						if($i%2) { $tr_color = "1";	} else { $tr_color = "2"; }
						include ('functions.php');
						echo '<tr class="d'.$tr_color.'">';
						echo '<td>'.$row["name"].'</td><td>'.$row["rule_num"].'</td><td>'.$row["function"].'</td><td>'.$row["protocol"].'</td><td>'.$src_nets.'</td><td>'.$dst_nets.'</td><td>'.$dst_ports.'</td><td>'.$row["inactive"].'</td><td>'.$row["log"].'</td>';
						echo '</tr>';
					}
				}
			}
		} 		elseif ($row_all['type'] === 'group') {
				$list_name = "SELECT * FROM acl WHERE src_net = '$src_net'";
				$result_list = $mysqli->query($list_name);
				while ($row = $result_list->fetch_array(MYSQLI_ASSOC)) {
					$i++; 
					if($i%2) { $tr_color = "1";	} else { $tr_color = "2"; }	
					if ($row['fw_name'] === 'FWSM_INTERNE') {
						$row["dst_port"]=str_replace("log", "", $row["dst_port"]);
						echo '<tr class="d'.$tr_color.'">';
						echo '<td>'.$row["name"].'</td><td>'.$row["rule_num"].'</td><td>'.$row["function"].'</td><td>'.$row["protocol"].'</td><td>'.$row["src_net"].'</td><td>'.$row["dst_net"].'</td><td>'.$row["dst_port"].'</td><td>'.$row["inactive"].'</td><td>'.$row["log"].'</td>';
						echo '</tr>';
					}
					elseif ($row['fw_name'] === 'R75') {
						$new_num = $row['rule_num'];
						$list_num = "SELECT *, GROUP_CONCAT(DISTINCT CONCAT_WS(  ' : ', acl.src_net, object.ipaddr ) SEPARATOR '<br>') AS src_nets, GROUP_CONCAT( DISTINCT CONCAT_WS(  ' : ', acl.dst_net, object.ipaddr ) SEPARATOR '<br>') AS dst_nets, GROUP_CONCAT(acl.dst_port SEPARATOR '<br>') AS dst_ports FROM acl LEFT JOIN object ON acl.src_net = object.obj_name OR acl.dst_net = object.obj_name WHERE rule_num =  '$new_num';";
						$result_num = $mysqli->query($list_num);
						while ($row = $result_num->fetch_array(MYSQLI_ASSOC)) {
						$i++; 
						if($i%2) { $tr_color = "1";	} else { $tr_color = "2"; }
						include ('functions.php');
						echo '<tr class="d'.$tr_color.'">';
						echo '<td>'.$row["name"].'</td><td>'.$row["rule_num"].'</td><td>'.$row["function"].'</td><td>'.$row["protocol"].'</td><td>'.$src_nets.'</td><td>'.$dst_nets.'</td><td>'.$dst_ports.'</td><td>'.$row["inactive"].'</td><td>'.$row["log"].'</td>';
						echo '</tr>';
					}
				}
			}
		}
	}
}

/* SI DST NET SELECTED - CAS #2 SEUL */
elseif (empty($src_net) && !empty($dst_net)) {
$new_num = '0';
		$list_name_all = "SELECT * FROM object where obj_name = '$dst_net' OR ipaddr = '$dst_net' LIMIT 1";
		$result_list_all = $mysqli->query($list_name_all);
		echo $ARRAY_HEADER;
		while ($row_all = $result_list_all->fetch_array(MYSQLI_ASSOC)) {
			if ($row_all['type'] != 'group') {
				$list_name = "SELECT * FROM acl a JOIN object o ON a.dst_net = '$dst_net' OR dst_net = (select obj_name from object where ipaddr = '$dst_net') OR dst_net = (select ipaddr from object where obj_name = '$dst_net') OR a.dst_net = o.obj_name WHERE (o.ipaddr = '$dst_net') OR (o. obj_name = '$dst_net')";
				$result_list = $mysqli->query($list_name);
				while ($row = $result_list->fetch_array(MYSQLI_ASSOC)) {
					$i++; 
					if($i%2) { $tr_color = "1";	} else { $tr_color = "2"; }	
					if ($row['fw_name'] === 'FWSM_INTERNE') {
						$row["dst_port"]=str_replace("log", "", $row["dst_port"]);
						echo '<tr class="d'.$tr_color.'">';
						echo '<td>'.$row["name"].'</td><td>'.$row["rule_num"].'</td><td>'.$row["function"].'</td><td>'.$row["protocol"].'</td><td>'.$row["src_net"].'</td><td>'.$row["dst_net"].'</td><td>'.$row["dst_port"].'</td><td>'.$row["inactive"].'</td><td>'.$row["log"].'</td>';
						echo '</tr>';
					}
					elseif ($row['fw_name'] === 'R75') {
						$new_num = $row['rule_num'];
						$list_num = "SELECT *, GROUP_CONCAT(DISTINCT CONCAT_WS(  ' : ', acl.src_net, object.ipaddr ) SEPARATOR '<br>') AS src_nets, GROUP_CONCAT( DISTINCT CONCAT_WS(  ' : ', acl.dst_net, object.ipaddr ) SEPARATOR '<br>') AS dst_nets, GROUP_CONCAT(acl.dst_port SEPARATOR '<br>') AS dst_ports FROM acl LEFT JOIN object ON acl.src_net = object.obj_name OR acl.dst_net = object.obj_name WHERE rule_num =  '$new_num';";
						$result_num = $mysqli->query($list_num);
						while ($row = $result_num->fetch_array(MYSQLI_ASSOC)) {
						$i++; 
						if($i%2) { $tr_color = "1";	} else { $tr_color = "2"; }
						include ('functions.php');
						echo '<tr class="d'.$tr_color.'">';
						echo '<td>'.$row["name"].'</td><td>'.$row["rule_num"].'</td><td>'.$row["function"].'</td><td>'.$row["protocol"].'</td><td>'.$src_nets.'</td><td>'.$dst_nets.'</td><td>'.$dst_ports.'</td><td>'.$row["inactive"].'</td><td>'.$row["log"].'</td>';
						echo '</tr>';
					}
				}
			}
		} 			elseif ($row_all['type'] === 'group') {
				$list_name = "SELECT * FROM acl WHERE dst_net = '$dst_net'";
				$result_list = $mysqli->query($list_name);
				while ($row = $result_list->fetch_array(MYSQLI_ASSOC)) {
					$i++; 
					if($i%2) { $tr_color = "1";	} else { $tr_color = "2"; }	
					if ($row['fw_name'] === 'FWSM_INTERNE') {
						$row["dst_port"]=str_replace("log", "", $row["dst_port"]);
						echo '<tr class="d'.$tr_color.'">';
						echo '<td>'.$row["name"].'</td><td>'.$row["rule_num"].'</td><td>'.$row["function"].'</td><td>'.$row["protocol"].'</td><td>'.$row["src_net"].'</td><td>'.$row["dst_net"].'</td><td>'.$row["dst_port"].'</td><td>'.$row["inactive"].'</td><td>'.$row["log"].'</td>';
						echo '</tr>';
					}
					elseif ($row['fw_name'] === 'R75') {
						$new_num = $row['rule_num'];
						$list_num = "SELECT *, GROUP_CONCAT(DISTINCT CONCAT_WS(  ' : ', acl.src_net, object.ipaddr ) SEPARATOR '<br>') AS src_nets, GROUP_CONCAT( DISTINCT CONCAT_WS(  ' : ', acl.dst_net, object.ipaddr ) SEPARATOR '<br>') AS dst_nets, GROUP_CONCAT(acl.dst_port SEPARATOR '<br>') AS dst_ports FROM acl LEFT JOIN object ON acl.src_net = object.obj_name OR acl.dst_net = object.obj_name WHERE rule_num =  '$new_num';";
						$result_num = $mysqli->query($list_num);
						while ($row = $result_num->fetch_array(MYSQLI_ASSOC)) {
						$i++; 
						if($i%2) { $tr_color = "1";	} else { $tr_color = "2"; }
						include ('functions.php');
						echo '<tr class="d'.$tr_color.'">';
						echo '<td>'.$row["name"].'</td><td>'.$row["rule_num"].'</td><td>'.$row["function"].'</td><td>'.$row["protocol"].'</td><td>'.$src_nets.'</td><td>'.$dst_nets.'</td><td>'.$dst_ports.'</td><td>'.$row["inactive"].'</td><td>'.$row["log"].'</td>';
						echo '</tr>';
					}
				}
			}
		}
	}
}

/* CAS  #1 & #2 */

elseif (!empty($src_net) && !empty($dst_net)) {
$new_num = '0';
		
		$list_name = "SELECT * FROM acl where (src_net = '$src_net' AND dst_net = '$dst_net') OR (fw_name = 'R75' AND (src_net = '$src_net' OR dst_net = '$dst_net'))";
		$result_list = $mysqli->query($list_name);
		echo $ARRAY_HEADER;
		while ($row = $result_list->fetch_array(MYSQLI_ASSOC)) {
					$i++; 
					if($i%2) { $tr_color = "1";	} else { $tr_color = "2"; }	
					if ($row['fw_name'] === 'FWSM_INTERNE') {
						$row["dst_port"]=str_replace("log", "", $row["dst_port"]);
						echo '<tr class="d'.$tr_color.'">';
						echo '<td>'.$row["name"].'</td><td>'.$row["rule_num"].'</td><td>'.$row["function"].'</td><td>'.$row["protocol"].'</td><td>'.$row["src_net"].'</td><td>'.$row["dst_net"].'</td><td>'.$row["dst_port"].'</td><td>'.$row["inactive"].'</td><td>'.$row["log"].'</td>';
						echo '</tr>';
					}
					elseif ($row['fw_name'] === 'R75') {
						$new_num = $row['rule_num'];
						$list_num = "SELECT *, GROUP_CONCAT(DISTINCT CONCAT_WS(  ' : ', acl.src_net, object.ipaddr ) SEPARATOR '<br>') AS src_nets, GROUP_CONCAT( DISTINCT CONCAT_WS(  ' : ', acl.dst_net, object.ipaddr ) SEPARATOR '<br>') AS dst_nets, GROUP_CONCAT(acl.dst_port SEPARATOR '<br>') AS dst_ports FROM acl LEFT JOIN object ON acl.src_net = object.obj_name OR acl.dst_net = object.obj_name WHERE rule_num =  '$new_num' having src_nets LIKE '%$src_net%' AND dst_nets LIKE '%$dst_net%';";
						$result_num = $mysqli->query($list_num);
						while ($row = $result_num->fetch_array(MYSQLI_ASSOC)) {
						$i++; 
						if($i%2) { $tr_color = "1";	} else { $tr_color = "2"; }
						include ('functions.php');
						echo '<tr class="d'.$tr_color.'">';
						echo '<td>'.$row["name"].'</td><td>'.$row["rule_num"].'</td><td>'.$row["function"].'</td><td>'.$row["protocol"].'</td><td>'.$src_nets.'</td><td>'.$dst_nets.'</td><td>'.$dst_ports.'</td><td>'.$row["inactive"].'</td><td>'.$row["log"].'</td>';
						echo '</tr>';
					}
				}
			}
		}


echo "</tbody>";
echo "</table>";

?>
<br>
<a href="index.php">Accueil</a>
</div >
</body>
</html>
